{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import symforce\n",
    "\n",
    "symforce.set_symbolic_api(\"sympy\")\n",
    "symforce.set_log_level(\"warning\")\n",
    "\n",
    "import symforce.symbolic as sf\n",
    "from symforce.ops import StorageOps\n",
    "from symforce.ops import LieGroupOps\n",
    "\n",
    "epsilon = 1e-9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def storage_D_tangent(a):\n",
    "    \"\"\"\n",
    "    Computes the jacobian of the storage space of an element with respect to the tangent space around\n",
    "    that element.\n",
    "\n",
    "    This is exact, but can be slow because we take a symbolic limit.\n",
    "    \"\"\"\n",
    "    # Perturb a in the tangent space\n",
    "    tangent_dim = LieGroupOps.tangent_dim(a)\n",
    "    xi = sf.Matrix(tangent_dim, 1).symbolic(\"xi\")\n",
    "    a_perturbed = LieGroupOps.retract(a, xi)\n",
    "    a_perturbed_storage = sf.Matrix(StorageOps.to_storage(a_perturbed))\n",
    "\n",
    "    # Compute jacobian of storage wrt perturbation\n",
    "    storage_D_tangent = a_perturbed_storage.jacobian(xi)\n",
    "\n",
    "    # Take the limit as perturbation goes to zero\n",
    "    # NOTE: This can be very slow\n",
    "    for x in xi:\n",
    "        storage_D_tangent = storage_D_tangent.limit(x, 0)\n",
    "\n",
    "    return storage_D_tangent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def storage_D_tangent_approx(a, epsilon):\n",
    "    \"\"\"\n",
    "    Computes the jacobian of the storage space of an element with respect to the tangent space around\n",
    "    that element.\n",
    "\n",
    "    This is an approximation, but is much faster than storage_D_tangent. Note that the exact jacobian\n",
    "    can often be recovered with a call to nsimplify with the appropriate tolerance (though this requires\n",
    "    the use of sympy rather than symengine)\n",
    "    \"\"\"\n",
    "    # Perturb a in the tangent space\n",
    "    tangent_dim = LieGroupOps.tangent_dim(a)\n",
    "    xi = sf.Matrix(tangent_dim, 1).symbolic(\"xi\")\n",
    "    a_perturbed = LieGroupOps.retract(a, xi)\n",
    "    a_perturbed_storage = sf.Matrix(StorageOps.to_storage(a_perturbed))\n",
    "\n",
    "    # Compute jacobian of storage wrt perturbation\n",
    "    storage_D_tangent = a_perturbed_storage.jacobian(xi)\n",
    "\n",
    "    # Rather than computing the limit, we substitude a small value for xi to approximate the limit\n",
    "    # NOTE: This is much faster than taking the limit in sympy, but returns an approximation of the true\n",
    "    # jacobian.\n",
    "    assert epsilon != 0\n",
    "    storage_D_tangent = storage_D_tangent.subs(xi, epsilon * xi.one())\n",
    "\n",
    "    return storage_D_tangent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix = sf.Matrix23.symbolic(\"self\")\n",
    "display(storage_D_tangent_approx(matrix, epsilon))\n",
    "display(storage_D_tangent(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rot2 = sf.Rot2.symbolic(\"self\")\n",
    "display(storage_D_tangent_approx(rot2, epsilon))\n",
    "display(storage_D_tangent(rot2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rot3 = sf.Rot3.symbolic(\"self\")\n",
    "display(storage_D_tangent_approx(rot3, epsilon))\n",
    "display(storage_D_tangent(rot3))  # This will take a while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pose2 = sf.Pose2.symbolic(\"self\")\n",
    "display(storage_D_tangent_approx(pose2, epsilon))\n",
    "display(storage_D_tangent(pose2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pose3 = sf.Pose3.symbolic(\"self\")\n",
    "display(storage_D_tangent_approx(pose3, epsilon))\n",
    "display(storage_D_tangent(pose3))  # This will take a while"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
